package net.betou.cms.action.admin.main;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.cms.entity.main.Channel;
import net.betou.cms.entity.main.CmsUser;
import net.betou.cms.manager.main.ChannelMng;
import net.betou.cms.manager.main.CmsGroupMng;
import net.betou.cms.manager.main.CmsLogMng;
import net.betou.cms.manager.main.CmsRoleMng;
import net.betou.cms.manager.main.CmsSiteMng;
import net.betou.cms.manager.main.CmsUserMng;
import net.betou.common.web.RequestUtils;
import net.betou.common.web.ResponseUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

public class CmsAdminAbstract{
	protected String channelsAddJson( Integer siteId, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		List< Channel > channelList = channelMng.getTopList( siteId, false );
		model.addAttribute( "channelList", channelList );
		response.setHeader( "Cache-Control", "no-cache" );
		response.setContentType( "text/json;charset=UTF-8" );
		return "admin/channels_add";
	}

	protected String channelsEditJson( Integer userId, Integer siteId, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		List< Channel > channelList = channelMng.getTopList( siteId, false );
		CmsUser user = manager.findById( userId );
		model.addAttribute( "channelList", channelList );
		model.addAttribute( "channelIds", user.getChannelIds( siteId ) );
		response.setHeader( "Cache-Control", "no-cache" );
		response.setContentType( "text/json;charset=UTF-8" );
		return "admin/channels_edit";
	}

	protected void checkUserJson( HttpServletRequest request, HttpServletResponse response ) {
		String username = RequestUtils.getQueryParam( request, "username" );
		String pass;
		if( StringUtils.isBlank( username ) ){
			pass = "false";
		}else{
			pass = manager.usernameNotExist( username ) ? "true" : "false";
		}
		ResponseUtils.renderJson( response, pass );
	}

	protected void checkEmailJson( String email, HttpServletResponse response ) {
		String pass;
		if( StringUtils.isBlank( email ) ){
			pass = "false";
		}else{
			pass = manager.emailNotExist( email ) ? "true" : "false";
		}
		ResponseUtils.renderJson( response, pass );
	}

	@Autowired
	protected CmsSiteMng cmsSiteMng;
	@Autowired
	protected ChannelMng channelMng;
	@Autowired
	protected CmsRoleMng cmsRoleMng;
	@Autowired
	protected CmsGroupMng cmsGroupMng;
	@Autowired
	protected CmsLogMng cmsLogMng;
	@Autowired
	protected CmsUserMng manager;
}
